<template>
  <com-card>
    <div slot="com_title">修改密码</div>
    <div slot="com_content">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原密码">
          <el-input
            type="text"
            v-model="ruleForm.oldPwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="newPwd">
          <el-input
            type="password"
            v-model="ruleForm.newPwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </com-card>
</template>

<script>
import { api_editpwd, api_checkoldpwd } from "../../api/users";
import ComCard from "../../components/comCard";

export default {
  components: {
    ComCard,
  },

  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.newPwd !== "") {
          this.$refs.ruleForm.validateField("newPwd");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        newPwd: "",
        oldPwd: "",
      },

      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        newPwd: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    async submitForm(formName) {
      let rst = await api_checkoldpwd({ oldPwd: this.ruleForm.oldPwd });
      let { code, msg } = rst;
      if (code == "00" && this.ruleForm.oldPwd != "") {
        if (this.ruleForm.oldPwd != this.ruleForm.pass) {
          this.$refs[formName].validate(async (valid) => {
            if (valid) {
              let rst = await api_editpwd({ newPwd: this.ruleForm.newPwd });
              let { code, msg } = rst;
              if (code == 0) {
                alert("修改成功，请重新登陆");
                this.$router.replace("/login");
              }

              console.log(valid);
            } else {
              console.log("error submit!!");
              return false;
            }
          });
        } else {
          alert("原密码和现密码一致 ");
        }
      }
      console.log(rst);
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped></style>
